package com.shuaiche.yeguan.util;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.SCApplication;


/**
 * 吐司方法工具类
 * Created by baoyb on 2017/3/17.
 */

public class ToastShowUtils {
    private static Toast toast;
    private static Toast customToast;
    private View toastView;

    /**
     * 加载失败带图片的toast
     * 0
     * @param text
     */
    public static void showFailedToast(String text) {
        if (!StringUtils.isEmpty(text)) {
            showCustomToast(text, 0, 3, Toast.LENGTH_SHORT);
        }
    }
    public static void showFailedToast(int id) {
        showCustomToast(null, id, 3, Toast.LENGTH_SHORT);
    }

    /**
     * 加载成功带图片的toast
     * 1
     * @param text
     */
    public static void showSuccessToast(String text) {
        if (!StringUtils.isEmpty(text)) {
            showCustomToast(text, 0, 1, Toast.LENGTH_SHORT);
        }
    }
    public static void showSuccessToast(int id) {
        showCustomToast(null, id, 1, Toast.LENGTH_SHORT);
    }
    /**
     * 加载成功带图片的toast
     * 2
     * @param text
     */
    public static void showTipToast(String text) {
        if (!StringUtils.isEmpty(text)) {
            showCustomToast(text, 0, 2, Toast.LENGTH_SHORT);
        }
    }
    public static void showTipToast(int id) {
        showCustomToast(null, id, 2, Toast.LENGTH_SHORT);
    }


    /**
     * 自定义带图片的toast，居中显示
     * @param text
     * @param result
     * @param millisecond
     */
    public static void showCustomToast(String text, int id, int result, int millisecond) {
        View toastView = null;
        ImageView ivToast = null;
        TextView tvToast = null;

        if (customToast == null) {
            customToast = new Toast(SCApplication.getApplication());
            customToast.setGravity(Gravity.CENTER, 0, 0);
            customToast.setDuration(millisecond);
        }

        toastView = LayoutInflater.from(SCApplication.getApplication())
                .inflate(R.layout.sc_layout_toast, null);
        ivToast = (ImageView) toastView.findViewById(R.id.ivToast);
        tvToast = (TextView) toastView.findViewById(R.id.tvToast);
        if (result == 0) {
            ivToast.setVisibility(View.GONE);
        } else {
            ivToast.setVisibility(View.VISIBLE);
            if (result == 3) {
                ivToast.setImageResource(R.mipmap.ic_toast_failed);
            } else if (result == 1) {
                ivToast.setImageResource(R.mipmap.ic_toast_success);
            } else if (result == 2) {
                ivToast.setImageResource(R.mipmap.ic_toast_tip);
            }
        }

        text = text == null ? ResourceUtils.getString(SCApplication.getApplication(), id) : text;

        tvToast.setText(text);
        customToast.setView(toastView);
        customToast.show();
    }


    /**
     * 显示文字toast
     * @param text
     */
    public static void showTextToast(String text) {
        if (!TextUtils.isEmpty(text)) {
            if (toast == null) {
                toast = Toast.makeText(SCApplication.getApplication(), text, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
            } else {
                toast.setText(text);
            }
            toast.show();
        }
    }

    public static void showTextToast(int id) {
        if (toast == null) {
            toast = Toast.makeText(SCApplication.getApplication(),
                    ResourceUtils.getString(SCApplication.getApplication(), id),
                    Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(ResourceUtils.getString(SCApplication.getApplication(), id));
        }
        toast.show();
    }
}
